import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunName: "login_home_welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunName: "login_home_welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunName: "login_home_welcome" */ '../components/Welcome.vue')
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
const Users = () => import(/* webpackChunName: "users_rights_roles" */ '../components/Users.vue')
const Rights = () => import(/* webpackChunName: "users_rights_roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunName: "users_rights_roles" */ '../components/power/Roles.vue')
// import Users from '../components/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
const Goods = () => import(/* webpackChunName: "goods_categories_params" */ '../components/Goods.vue')
const Categories = () => import(/* webpackChunName: "goods_categories_params" */ '../components/Categories.vue')
const Params = () => import(/* webpackChunName: "goods_categories_params" */ '../components/Params.vue')
// import Goods from '../components/Goods.vue'
// import Categories from '../components/Categories.vue'
// import Params from '../components/Params.vue'
const Add = () => import(/* webpackChunName: "add_order_report" */ '../components/Add.vue')
const Order = () => import(/* webpackChunName: "add_order_report" */ '../components/Order.vue')
const Report = () => import(/* webpackChunName: "add_order_report" */ '../components/Report.vue')
// import Add from '../components/Add.vue'
// import Order from '../components/Order.vue'
// import Report from '../components/Report.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/goods',
        component: Goods
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/add',
        component: Add
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由导航路由守卫
router.beforeEach((to, from, next) => {
  // 如果请求路径是登陆路径,就继续
  if (to.path === '/login') return next()
  // 如果请求路径不是登录路径,将sessionStorage保存的token取出来做判断
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果token为空,则返回登录页面
  if (!tokenStr) return next('/login')
  // 浏览页面
  next()
})

export default router
